package com.tubruk.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.tubruk.object.ConstantList;
import com.tubruk.object.IOption;
import com.tubruk.object.Option;
import com.tubruk.object.option.ListOption;

public class OptionDB {
	
	public static int updateValueByPK(Option option){
		int result = -1;
		
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.TABLE_CONSTANT.VALUE, option.getValue());
		
		DatabaseHelper db = null;
		
		try {
			db = new DatabaseHelper();
			result = db.update(DatabaseHelper.TABLE_CONSTANT.TABLENAME, values, 
					 	DatabaseHelper.TABLE_CONSTANT.ID + "=" + option.getId(), null);
		}finally {
			if(db != null) {
				db.destroy();
			}
		}
		
		
		return result;
	}
	
	public static ArrayList<IOption> getAllOptions(){
		ArrayList<IOption> result = null;

		DatabaseHelper db = null;

		try {
			db = new DatabaseHelper();
			Cursor c = db.query(DatabaseHelper.TABLE_CONSTANT.TABLENAME, null, null, null, "_id asc");

			if (c != null){
				result = new ArrayList<IOption>();
				if (c.moveToFirst()){
					do {
						result.add(convertDBToOption(c));
					}while (c.moveToNext());
				}
			}
		}finally {
			if (db != null){
				db.destroy();
			}
		}
		return result;
	}
	
	public static IOption getOptionById(int id){
		IOption result = null;

		DatabaseHelper db = null;
		try {
			db = new DatabaseHelper();
			//String[] selectionArgs = {Integer.toString(constantId)};
			
			Cursor c = db.query(DatabaseHelper.TABLE_CONSTANT.TABLENAME, null, DatabaseHelper.TABLE_CONSTANT.ID + "=" + id, null, null);
	
			if (c != null){
				if (c.moveToFirst()){
					result = convertDBToOption(c);
				}
			}
		}finally {
			if (db != null) {
				db.destroy();
			}
		}
		return result;

	}
	
	public static String getValueById(int id){
		String result = null;
		IOption option = OptionDB.getOptionById(id);
		if (option != null) {
			result = option.getValue();
		}	
		return result;
	}

	private static IOption convertDBToOption(Cursor c){
		IOption result = null;

		int mode = c.getInt (c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.MODE));
		if (mode == DatabaseHelper.TABLE_CONSTANT.MODE_LIST){
			ListOption option = new ListOption();
			option.setId(c.getInt(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.ID)));
			option.setTitle(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.TITLE)));
			option.setDescription(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.DESCRIPTION)));
			option.setMode(mode);
			option.setValue(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.VALUE)));
			

			option.setAllowableValues(ConstantListDB.getByConstantId(option.getId()));

			result = option;
		}else {
			Option option = new Option();
			option.setId(c.getInt(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.ID)));
			option.setTitle(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.TITLE)));
			option.setDescription(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.DESCRIPTION)));
			option.setMode(mode);
			option.setValue(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_CONSTANT.VALUE)));

			result = option;
		}

		return result;
	}
}
